<?php
/**
 * @author 天涯 <mail@phpha.com>
 * @copyright http://blog.phpha.com
 * @link http://blog.phpha.com/vncms
 * @version 1.0
 */
defined('APP_PATH') or exit('Access Denied');
require 'check.php';
class table extends check{
	//模型列表
	public function lis(){
		$this->table = spClass('m_table')->findAll(null,'modelid ASC');
		$this->display('table_lis.tpl.php');
	}
	//添加模型
	public function add(){
		$this->display('table_add.tpl.php');
	}
	public function doadd(){
		if(false != spClass('m_table')->spVerifier($this->spArgs())){
			$this->error('数据验证错误！',spUrl('table','add'));
		}
		if(1 == spClass('m_table')->check_table(trim($this->spArgs('tablename')))){
			$this->error('表名已存在！',spUrl('table','add'));
		}
		if(false != ($modelid = spClass('m_table')->create($this->spArgs()))){
			//创建数据表
			$tablename = $GLOBALS['G_SP']['db']['prefix'] . trim($this->spArgs('tablename'));
			$sql = "CREATE TABLE `{$GLOBALS['G_SP']['db']['database']}`.`{$tablename}` (
					`id` INT( 10 ) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY ,
					`cid` TINYINT( 3 ) UNSIGNED NOT NULL ,
					`title` VARCHAR( 100 ) NOT NULL ,
					`keywords` VARCHAR( 200 ) NOT NULL ,
					`description` TEXT NOT NULL ,
					`content` LONGTEXT NOT NULL ,
					`addtime` INT( 10 ) UNSIGNED NOT NULL ,
					`views` INT( 10 ) UNSIGNED NOT NULL ,
					`stick` TINYINT( 1 ) UNSIGNED NOT NULL ,
					`odnum` TINYINT( 3 ) UNSIGNED NOT NULL ,
					`visible` TINYINT( 1 ) UNSIGNED NOT NULL ,
					`thumb` VARCHAR( 100 ) NOT NULL
				) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci;";
			spClass('m_table')->query($sql);
			//更新字段表
			$fields = array(
				array('modelid' => $modelid, 'field' => 'id', 'name' => '编号', 'typeid' => 3, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'cid', 'name' => '分类编号', 'typeid' => 1, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'title', 'name' => '标题', 'typeid' => 7, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'keywords', 'name' => '关键词', 'typeid' => 7, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'description', 'name' => '描述信息', 'typeid' => 8, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'content', 'name' => '内容', 'typeid' => 10, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'addtime', 'name' => '创建时间', 'typeid' => 4, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'views', 'name' => '浏览次数', 'typeid' => 3, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'stick', 'name' => '是否置顶', 'typeid' => 5, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'odnum', 'name' => '排序编号', 'typeid' => 1, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'visible', 'name' => '是否可见', 'typeid' => 5, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段'),
				array('modelid' => $modelid, 'field' => 'thumb', 'name' => '缩略图', 'typeid' => 7, 'value' => '', 'odnum' => 0, 'canupd' => 0, 'tips' => '系统字段')
			);
			foreach($fields as $v){
				spClass('m_field')->create($v);
			}
			$this->success('添加模型成功！',spUrl('table','lis'));
		}else{
			$this->error('添加模型失败！',spUrl('table','add'));
		}
	}
	//更新模型
	public function upd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		$this->table = spClass('m_table')->find(array('modelid'=>$id)); 
		$this->display('table_upd.tpl.php');
	}
	public function doupd(){
		if(!$id = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		if(false != spClass('m_table')->update(array('modelid'=>$id),$this->spArgs())){
			$this->success('更新模型成功！',spUrl('table','lis'));
		}else{
			$this->error('更新模型失败！',spUrl('table','lis'));
		}
	}
	//删除模型
	public function del(){
		if(!$modelid = intval($this->spArgs('id')))	$this->error('缺少必要参数！',spUrl('table','lis'));
		if(false != spClass('m_zzcat')->find(array('modelid'=>$modelid))){
			$this->error('存在栏目禁止删除！',spUrl('zzcat','lis'));
		}
		$table = spClass('m_table')->find(array('modelid'=>$modelid));
		$tablename = $GLOBALS['G_SP']['db']['prefix'] . $table['tablename'];
		if(false != spClass('m_table')->delete(array('modelid'=>$modelid))){
			spClass('spModel')->query("DROP TABLE IF EXISTS `{$tablename}`");
			$this->success('删除模型成功！',spUrl('table','lis'));
		}else{
			$this->error('删除模型失败！',spUrl('table','lis'));
		}
	}
}
